package com.ibeeking.found.log.rest.service.impl;

import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.log.rest.service.ILogCodeGroupService;
import com.ibeeking.found.log.rest.service.ILogCodeService;
import com.ibeeking.found.log.service.common.param.LogCodeGroupAddByCodeParam;
import com.ibeeking.found.log.service.common.query.LogCodeGroupQuery;
import com.ibeeking.found.log.service.common.vo.LogCodeGroupVO;
import com.ibeeking.found.log.service.mapper.LogCodeMapper;
import com.ibeeking.found.log.service.common.param.LogCodeParam;
import com.ibeeking.found.log.service.common.vo.LogCodeVO;
import com.ibeeking.found.log.service.common.dos.LogCodeDO;
import com.ibeeking.found.log.service.common.query.LogCodeQuery;
import com.ibeeking.found.log.service.common.query.LogCodePageQuery;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.ParamCheckUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @ClassName LogCodeServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-25 16:42
 **/
@Service
public class LogCodeServiceImpl extends ServiceImpl<LogCodeMapper, LogCodeDO> implements ILogCodeService {

    @Resource
    private LogCodeMapper logCodeMapper;
    @Resource
    private ILogCodeGroupService logCodeGroupServiceImpl;

    @Override
    public List<LogCodeVO> list(LogCodeQuery logCodeQuery) {
        List<LogCodeDO> logCodeDOs = logCodeMapper.selectList(new LambdaQueryWrapper<LogCodeDO>().like(StringUtils.isNotBlank(logCodeQuery.getCode()), LogCodeDO::getCode, logCodeQuery.getCode()).like(StringUtils.isNotBlank(logCodeQuery.getDescription()), LogCodeDO::getDescription, logCodeQuery.getDescription()).orderByDesc(LogCodeDO::getCreateTime));
        return JsonUtils.jsonToList(logCodeDOs, LogCodeVO.class);
    }

    @Override
    public Page<LogCodeVO> pageList(LogCodePageQuery logCodePageQuery) {
        Page<LogCodeDO> pages = logCodeMapper.selectPage(new Page<LogCodeDO>(logCodePageQuery.getPageNum(), logCodePageQuery.getPageSize()), new LambdaQueryWrapper<LogCodeDO>().like(StringUtils.isNotBlank(logCodePageQuery.getCode()), LogCodeDO::getCode, logCodePageQuery.getCode()).like(StringUtils.isNotBlank(logCodePageQuery.getDescription()), LogCodeDO::getDescription, logCodePageQuery.getDescription()).orderByDesc(LogCodeDO::getCreateTime));
        return BeanUtil.convertPage(pages, LogCodeVO.class);
    }

    @Override
    public Boolean add(LogCodeParam logCodeParam) {
        //id设为空//去除首尾空格
        logCodeParam.setId(null);
        ParamCheckUtils.stringTrim(logCodeParam);
        //校验唯一性
        this.checkUnique(logCodeParam);
        LogCodeDO logCodeDO = BeanUtil.convertBean(logCodeParam, LogCodeDO.class);
        logCodeDO.setCreateBy(UserUtils.getUserId());
        logCodeDO.setCreateTime(LocalDateTime.now());
        logCodeMapper.insert(logCodeDO);
        LogCodeGroupAddByCodeParam param = new LogCodeGroupAddByCodeParam();
        param.setCodeId(logCodeDO.getId());
        param.setGroupIdList(logCodeParam.getGroupIdList());
        logCodeGroupServiceImpl.addByCodeId(param);
        return true;
    }

    @Override
    public Boolean modify(LogCodeParam logCodeParam) {
        ParamCheckUtils.stringTrim(logCodeParam);
        //校验唯一性
        this.checkUnique(logCodeParam);
        LogCodeDO logCodeDO = BeanUtil.convertBean(logCodeParam, LogCodeDO.class);
        logCodeDO.setModifyBy(UserUtils.getUserId());
        logCodeDO.setModifyTime(LocalDateTime.now());
        logCodeMapper.updateById(logCodeDO);
        LogCodeGroupAddByCodeParam param = new LogCodeGroupAddByCodeParam();
        param.setCodeId(logCodeDO.getId());
        param.setGroupIdList(logCodeParam.getGroupIdList());
        logCodeGroupServiceImpl.addByCodeId(param);
        return true;
    }

    @Override
    public LogCodeVO view(Long id) {
        LogCodeDO logCodeDO = logCodeMapper.selectById(id);
        LogCodeGroupQuery logCodeGroupQuery = new LogCodeGroupQuery();
        logCodeGroupQuery.setCodeId(id);
        List<LogCodeGroupVO> list = logCodeGroupServiceImpl.list(logCodeGroupQuery);
        List<Long> groupIdList = list.stream().map(LogCodeGroupVO::getGroupId).collect(Collectors.toList());
        LogCodeVO logCodeVO = BeanUtil.convertBean(logCodeDO, LogCodeVO.class);
        Optional.ofNullable(logCodeVO).ifPresent(x -> x.setGroupIdList(groupIdList));
        return logCodeVO;
    }

    @Override
    public Boolean del(Long id) {
        logCodeGroupServiceImpl.delByCodeId(id);
        return logCodeMapper.deleteById(id) > 0;
    }


    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(LogCodeParam param) {
        //code唯一性
        LambdaQueryWrapper<LogCodeDO> queryWrapper = new LambdaQueryWrapper<LogCodeDO>()
                .ne(param.getId() != null, LogCodeDO::getId, param.getId())
                .eq(LogCodeDO::getCode, param.getCode());
        List<LogCodeDO> logCodeDOS = logCodeMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(logCodeDOS)) {
            throw BusinessException.create("已经存在相同的日志编码!");
        }
    }
}